import json
import random
import time
import pymysql
from playwright.sync_api import sync_playwright  # 同步模式
from retrying import retry

'''
https://we.51job.com/api/job/search-pc?api_key=51job&timestamp=1710994840&keyword=&searchType=2&function=&industry=&jobArea=000000&jobArea2=&landmark=&metro=&salary=&workYear=&degree=&companyType=&companySize=&jobType=&issueDate=&sortType=0&pageNum=2&requestId=fb5795dad2c0ceae8746cbd938c71b59&pageSize=20&source=1&accountId=&pageCode=sou%7Csou%7Csoulb
'''
connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', password='123456', db='jobinformation',
                             charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor)


@retry(stop_max_attempt_number=10, retry_on_result=lambda x: x is False, wait_fixed=3000)
# 这个装饰器的作用就是在滑块失败的时候可以重复滑动
def Sync_Playwright(url):
    """处理滑块"""
    with sync_playwright() as fp:
        bs = fp.firefox.launch(headless=True)  # 禁用无头模式(也就是启动不启动浏览器的区别)
        page = bs.new_page()  # 新建选项卡
        page.goto(url)  # 加载页面
        dropbutton = page.locator('#nc_1_n1z')
        box = dropbutton.bounding_box()  # 获取其边界框
        page.mouse.move(box['x'] + box['width'] / 2, box['y'] + box['height'] / 2)
        page.mouse.down()
        # 模拟人类的鼠标移动轨迹
        steps = 30  # 可以根据需要调整步数
        for step in range(1, steps + 1):
            progress = step / steps
            mov_x = box['x'] + box['width'] / 2 + 300 * progress + random.uniform(-5, 5)
            mov_y = box['y'] + box['height'] / 2 + random.uniform(-5, 5)
            page.mouse.move(mov_x, mov_y)
            time.sleep(random.uniform(0.02, 0.1))  # 模拟人的自然反应时间
        page.mouse.up()
        ttt = random.uniform(5, 8)
        time.sleep(ttt)
        html = page.locator('xpath=/html/body/pre')
        qwer = html.inner_text()
        if 'resultbody' in qwer:
            a = json.loads(qwer)
            return a
        else:
            return False


def parseDataFields(datas):
    # try:
    data = datas['resultbody']['job']['items']
    cursor = connection.cursor()
    for i in data:
        """解析数据"""
        salary = str(i['provideSalaryString'])
        companyLabelList = '，'.join([str(i) for i in list(i['jobTags'])])
        jobDescribe = str(i['jobDescribe']).replace('\n', '').replace('\r', '')
        sql = (
            "INSERT INTO `qianchengwuyou_table_2` VALUES ('{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}','{}')"
            .format(i['jobId'], i['companyName'], i['jobAreaString'] + i['fullCompanyName'], salary,
                    i['degreeString'], i['workYearString'], i['industryType1Str'], jobDescribe,
                    i['jobName'], i['jobAreaString'], i['companySizeString'], companyLabelList))

        print(i['jobId'], i['companyName'], i['jobAreaString'] + i['fullCompanyName'], salary,
              i['degreeString'], i['workYearString'], i['industryType1Str'], jobDescribe,
              i['jobName'], i['jobAreaString'], i['companySizeString'], companyLabelList)
        try:
            cursor.execute(sql)
        except:
            pass
    connection.commit()
    cursor.close()

    # except:
    #     pass


if __name__ == '__main__':
    # 获取当前时间的时间戳（秒）
    timestamp = int(time.time())
    # 将时间戳转换为指定格式的字符串
    formatted_timestamp = "{:06d}".format(timestamp)
    # 总数10
    for page in range(1, 11):
        url = (
            f'https://we.51job.com/api/job/search-pc?api_key=51job&timestamp={formatted_timestamp}&'
            f'keyword=%E8%AE%A1%E7%AE%97%E6%9C%BA&searchType=2&function=&industry=01%2C37%2C38%2C31%2C40&jobArea=&jobArea2='
            f'&landmark=&metro=&salary=&workYear=&degree=&companyType=&companySize=&jobType='
            f'&issueDate=&sortType=3&pageNum={page}&requestId=71bfbc17ec519737773f2b7e039f1d27'
            f'&pageSize=100&source=1.txt&accountId=&pageCode=sou%7Csou%7Csoulb')
        print(url)
        html = Sync_Playwright(url)
        parseDataFields(html)
    connection.close()
